import { postAutohorityRewardApi } from '@/services/api';
import {
  ModalForm,
  ProFormDigit,
  ProFormInstance,
} from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { isEqual } from 'lodash';
import React, { useEffect, useRef } from 'react';

interface Props {
  open?: boolean;
  info?: any;
  close?: () => void;
  onFinsh?: () => void;
  options?: any;
}
// 新增/编辑用户
const BindConfigDrawer: React.FC<Props> = ({
  open,
  close,
  info,
  onFinsh,
  options,
}) => {
  const [form] = Form.useForm<{ name: string; company: string }>();

  const formRef = useRef<ProFormInstance>();

  console.log('options', options);

  // useEffect(() => {
  //   if (info) {
  //     setTimeout(() => {
  //       formRef?.current?.setFieldsValue({
  //         ...info,
  //       });
  //     }, 20);
  //   }
  // }, [info]);

  return (
    <ModalForm
      formRef={formRef}
      title={'奖励设置'}
      form={form}
      open={open}
      width={600}
      onOpenChange={(visible) => {
        if (!visible) {
          close?.();
        }
      }}
      autoFocusFirstInput
      onFinish={async (values) => {
        try {
          const { msg } = await postAutohorityRewardApi(info?.id, {
            ...values,
          });
          onFinsh?.();
          message.success(msg);
          // 不返回不会关闭弹框
          return true;
        } catch (error) {}
      }}
      initialValues={{
        ...info,
      }}
    >
      <ProFormDigit
        name="money_one"
        label="发展奖励"
        rules={[
          {
            required: true,
          },
        ]}
      ></ProFormDigit>

      <ProFormDigit
        name="money_two"
        label="单电奖励"
        rules={[
          {
            required: true,
          },
        ]}
      ></ProFormDigit>

      <ProFormDigit
        name="money_three"
        label="车电奖励"
        rules={[
          {
            required: true,
          },
        ]}
      ></ProFormDigit>
    </ModalForm>
  );
};

export default React.memo(BindConfigDrawer, (prev, next) =>
  isEqual(prev, next),
);
